//
//  LYMenu.m
//  lottery
//
//  Created by 赵国腾 on 16/6/11.
//  Copyright © 2016年 赵国腾. All rights reserved.
//

#import "LYDownMenu.h"
#import "LYMenuItem.h"

#define LYCols 3

@interface LYDownMenu ()

@property (nonatomic, strong) NSArray *items;
@property (nonatomic, strong) NSMutableArray *buttons;

@end

@implementation LYDownMenu

- (NSMutableArray *)buttons {
    
    if (_buttons == nil) {
        _buttons = [NSMutableArray array];
    }
    
    return _buttons;
}

+ (instancetype)showInView:(UIView *)view items:(NSArray<LYMenuItem *> *)items oriY:(CGFloat)oriY  {
    
    NSInteger count = items.count;
    
    if (count % 3) {
        NSException * exc = [NSException exceptionWithName:@"参数不符合" reason:@"传入的数组总数必须是3的倍数!" userInfo:nil];
        
        [exc raise];
    }
    
    NSInteger rows = (count -1)/ LYCols + 1;
    
    CGFloat itemWH = LYScreenBoundsW / LYCols;
    CGFloat menuH = rows * itemWH;
    
    LYDownMenu *downMenu = [[LYDownMenu alloc] initWithFrame:CGRectMake(0, oriY, LYScreenBoundsW, menuH)];
    
    downMenu.items = items;
    downMenu.backgroundColor = [UIColor blackColor];
    
    [downMenu setupAllBtns];
    
    [downMenu setupAllLines];
    
    // 添加黑色view
    UIView *blackView = [[UIView alloc] initWithFrame:downMenu.frame];
    
    blackView.backgroundColor = [UIColor blackColor];
    [view addSubview:blackView];
    
    [view addSubview:downMenu];
    
    downMenu.transform = CGAffineTransformMakeTranslation(0, -downMenu.gt_height);
    
    // 添加动画
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        
        downMenu.transform = CGAffineTransformIdentity;
        
    } completion:^(BOOL finished) {
        
        [blackView removeFromSuperview];
    }];
    
    return downMenu;
}

+ (instancetype)showInWindowWithItems:(NSArray<LYMenuItem *> *)items oriY:(CGFloat)oriY {
    
    return [LYDownMenu showInView:LYKeyWindow items:items oriY:oriY];
}

/** 添加按钮 */
- (void)setupAllBtns {
    
    for (LYMenuItem *item in self.items) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [button setImage:item.image forState:UIControlStateNormal];
        [button setTitle:item.title forState:UIControlStateNormal];
        
        [self addSubview:button];
        [self.buttons addObject:button];
    }
}

// 添加分割线
- (void)setupAllLines {
    
    CGFloat itemWH = LYScreenBoundsW / LYCols;
    NSInteger row = self.buttons.count / LYCols;
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat w = 0.5;
    CGFloat h = self.gt_height;

    // 竖线
    for (NSInteger i = 0; i < LYCols - 1; i++) {
        
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = [UIColor whiteColor];

        x = (i + 1) *itemWH;
        y = 0;
        
        lineView.frame = CGRectMake(x, y, w, h);
        
        [self addSubview:lineView];
    }
    
    // 横线
    w = self.gt_width;
    h = 0.5;
    for (NSInteger i = 0; i < row - 1; i++) {
        
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = [UIColor whiteColor];
        
        x = 0;
        y = (i + 1) *itemWH;
        
        lineView.frame = CGRectMake(x, y, w, h);
        
        [self addSubview:lineView];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    NSInteger count = self.items.count;
    
    NSInteger col = 0;
    NSInteger row = 0;
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat w = LYScreenBoundsW / LYCols;
    CGFloat h = w;

    for (NSInteger i = 0; i < count; i++) {
        
        col = i % LYCols;
        row = i / LYCols;
        x = col * w;
        y = row * h;

        UIButton *button = self.buttons[i];
        button.frame = CGRectMake(x, y, w, h);
    }
}

+ (void)hide {
    
    for (UIView *view in LYKeyWindow.subviews) {
        
        if ([view isKindOfClass:[LYDownMenu class]]) {
            
            [view removeFromSuperview];
        }
    }
    
}

- (void)hide {
    
    [UIView animateWithDuration:0.5 animations:^{
        
        self.transform = CGAffineTransformMakeTranslation(0, -self.gt_height);
    }completion:^(BOOL finished) {
        
        [self removeFromSuperview];
    }];
    
}

@end





















